﻿using An_Im.Extension.Contract;
using An_Im.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace An_Im.UI
{
    /// <summary>
    /// AnImCommandViewControl.xaml 的交互逻辑
    /// </summary>
    public partial class AnImCommandViewControl : UserControl
    {
        public AnImCommandViewControl()
        {
            InitializeComponent();
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            var control = sender as FrameworkElement;
            if (control == null) return;
            var command = control.DataContext as IAnImCommand;
            command.Excute(CoreManipulator.App);
        }

        private void addVBAItem_Click(object sender, RoutedEventArgs e)
        {
            var control = sender as MenuItem;
            var command = control.DataContext as IAnImCommand;
            if (command.VBAExportable == false) return;
            var vbacode = command.ExportVBACode();

            if (command.AddIn != null)
            {
                var addin = command.AddIn;
                if( VBAManipulator.FindComponentByName(addin.VBAReference.BaseAPIModuleName) == null)
                    VBAManipulator.AddComponent(addin.VBAReference.BaseAPIModuleName, addin.VBAReference.BaseAPIModuleCode,true);
            }

            VBAManipulator.Write("main", vbacode);
        }
    }
}
